<?php
class DB {

	private $driver;
	private $cid;
	private $database;
	private $username;
	private $password;

	function __construct($username,$password,$database,$host,$driver){
		$this->driver = $driver;
		$this->database = $database;
		$this->username = $username;
		$this->password = $password;
		$this->host = $host;
		$this->dbConnect();
	}

	public function returnList($query){
		$nav = array();
		$result = $this->dbQuery($query);
		$i = 0;
		while ($row = $this->dbFetchAssoc($result)) {
			foreach ($row as $key => $value) {
				$nav[$i][$key] = $value;
			}
			$i++;
		}
		return $nav;
	}


	public function returnInfo($query){ 
		$result = $this->dbQuery($query);
		$array = array();
		while ($row = $this->dbFetchAssoc($result)) 
			foreach ($row as $key => $value) 
			{
				$array[$key] = $value;
			}
		return $array;
	}

	public function execute($query){
		$no_err = true;
		$result = $this->dbQuery($query);
		if (!$result){
			if( ($errors = sqlsrv_errors() ) != null) {
				foreach( $errors as $error ) {
					echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
					echo "code: ".$error[ 'code']."<br />";
					echo "message: ".$error[ 'message']."<br />";
				}
			}		
			$no_err = false;
			$err = $this->returnInfo('select @@error;');
			
			if (!empty($err) && $err[0]['code']!='5701' && $err[0]['code']!='5703'){
				//print_r($err);
			}
			die("Execution failed. Query: ".$query);
		}
		return $no_err;
	}


	/*---------------------------------
	Call database PHP methods according to driver
	---------------------------------*/

	/* Connect to Database */

	private function dbConnect (){
		switch ($this->driver) {
			case "MSSQL":
			    //echo $host;
				$this->cid = mssql_connect($this->host,$this->username,$this->password,true);
				mssql_select_db("[".$this->database."]",$this->cid) ; 
				break;
			case "SYBASE":
				$this->cid = sybase_connect($this->host,$this->username,$this->password,true);
				sybase_select_db($this->database,$this->cid) ; 
				break;
			case "ODBC":
				$this->cid = new PDO('odbc:'.$this->host);				
				break;
			case "ODBC2":
				$this->cid = odbc_connect($this->host,$this->username,$this->password);	
				//$this->dbQuery ("use " . $this->database);
				break;	
			case "SQLSRV":
				$connectionInfo = array( "Database"=>$this->database, "UID"=>$this->username, "PWD"=>$this->password);
				$this->cid = sqlsrv_connect( $this->host, $connectionInfo);
				break;				
		}
//		if (!$this->cid) {
//			die ("Neprisijungta prie ".$this->driver." serverio");
//		}
	}

	public function isConnected () {
		if (!$this->cid) {
			return false;
		} else {
			return true;
		}
	}
	
	/* Query server */


	private function dbQuery ($query){
		switch ($this->driver) {
			case "MSSQL":
				$result = mssql_query($query,$this->cid);
				break;
			case "SYBASE":
				$result = sybase_query($query,$this->cid); 
				break;
			case "ODBC":
				$result = $this->cid->query($query); 
				break;		
			case "ODBC2":
				$result = odbc_exec($this->cid,$query); 
				break;	
			case "SQLSRV":
				$result = sqlsrv_query($this->cid,$query); 
				break;					
		}
		return $result;
	}


	/* Fetch assoc*/

	private function dbFetchAssoc($result){
		switch ($this->driver) {
			case "MSSQL":
				$row = mssql_fetch_assoc($result);
				break;
			case "SYBASE":
				$row = sybase_fetch_assoc($result);
				break;
			case "ODBC":
				$row = $result->fetch(PDO::FETCH_ASSOC);
				break;	
			case "ODBC2":
				$row = odbc_fetch_array($result);
				//print_r($row);
				break;
			case "SQLSRV":
				$row = sqlsrv_fetch_array($result);
				break;				
		}
		return $row;
	}


	function __call($method,$arguments){
		echo ('undefined method was called. Method: '.$method.', with the fallowing argumets:<br /><pre>');
		print_r($arguments);
		echo ('</pre>');
	}
}

?>